package example;

import config.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import po.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by adaptercat on 17-6-15.
 */
public class Mybatis_main {

    //  根据id查询用户信息，得到一条记录的结果
    @Test
    public void findUseiyIdTest() throws IOException {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //  创建会话工厂 传入mybatis的配置信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //  通过工厂得到SQLsession

        SqlSession sqlSession = sqlSessionFactory.openSession();

        //  通过sqlSession操作数据库

        //  第一个参数 映设文件中的statement的id 等于namespace.+statement的id
        //  第二个参数 指定和映设文件中的匹配参数paramenterType
        User user = sqlSession.selectOne("test.findUsserByID", 1);
        System.out.println(user);

        //  释放资源
        sqlSession.close();
    }

    @Test
    public void findUserByNameTest() throws IOException {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //  创建会话工厂 传入mybatis的配置信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //  通过工厂得到SQLsession

        SqlSession sqlSession = sqlSessionFactory.openSession();

        //  通过sqlSession操作数据库

        //  第一个参数 映设文件中的statement的id 等于namespace.+statement的id
        //  第二个参数 指定和映设文件中的匹配参数paramenterType
        List<User> resultData = sqlSession.selectList("test.findUserByName", "羽");
        System.out.println(resultData);

        //  释放资源
        sqlSession.close();
    }

    @Test
    public void insertUsserTest() throws IOException {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //  创建会话工厂 传入mybatis的配置信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //  通过工厂得到SQLsession

        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user = new User();
        user.setUsername("曹操");
        user.setSex("男");
        user.setAddress("魏国");
        sqlSession.insert("test.insertUser", user);
        //System.out.println();
        sqlSession.commit();
        //  获取用户信息的主键
        System.out.println(user);

        //  释放资源
        sqlSession.close();
    }

    @Test
    public void deleteUsserTest() throws IOException {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //  创建会话工厂 传入mybatis的配置信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //  通过工厂得到SQLsession

        SqlSession sqlSession = sqlSessionFactory.openSession();

//        User user = new User();
//        user.setUsername("曹操");
//        user.setSex("男");
//        user.setAddress("魏国");
        sqlSession.delete("test.deleteUser", 9);
        //System.out.println();
        sqlSession.commit();
        //  获取用户信息的主键
//        System.out.println(user);

        //  释放资源
        sqlSession.close();
    }

    @Test
    public void updateUsserTest() throws IOException {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //  创建会话工厂 传入mybatis的配置信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //  通过工厂得到SQLsession

        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user = new User();
        user.setId(8);
        user.setUsername("貂蝉");
        user.setSex("女");
        user.setAddress("在野");
        sqlSession.update("test.updateUser", user);
        //System.out.println();
        sqlSession.commit();
        //  获取用户信息的主键
        System.out.println(user);

        //  释放资源
        sqlSession.close();
    }


    @Test
    public void TestMapper() throws Exception {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.findUsserByID(1);
      System.out.println(user);
    }


}
